import axios from 'axios'
import qs from 'qs'
import cookie from 'js-cookie'
axios.defaults.cookie='JSESSIONID=9903A4F10A6CEBA97E2E69D6A327EAE7'
const http = axios.create({
    // baseURL: 'https://some-domain.com/api/',
    timeout: 10000000,
    // headers: { 'X-Custom-Header': 'foobar' }
});


// Add a request interceptor
http.interceptors.request.use(function (config) {
    
    const sessToken=sessionStorage.getItem('token')
    if(sessToken){
        config.headers={
            'Authorization':"Bearer "+sessionStorage.getItem("token"),
        }
    }

    if (config.data && !config.data instanceof FormData && config.data !== 'string') {
        // config.data.token = cookie.get('token');
        // config.headers['Authorization'] = 'Bearer ' +sessionStorage.getItem('token')
      config.headers={
    'Authorization':"Bearer "+sessionStorage.getItem("token"),
}
        config.data = qs.stringify(config.data)
    }
    // config.headers.Authorization=sessionStorage.getItem('token')
    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

// Add a response interceptor
http.interceptors.response.use(function (response) {
    // Do something with response data
    return response.data;
}, function (error) {
    // Do something with response error
    return Promise.reject(error);
});

export default http




// import axios from 'axios'
// import qs from 'qs'

// const http = axios.create({
//     // baseURL: 'https://some-domain.com/api/',
//     timeout: 1000,
//     // headers: { 'X-Custom-Header': 'foobar' }
// });

// // 开启跨域请求携带cookie
// http.defaults.withCredentials = true;

// // Add a request interceptor
// http.interceptors.request.use(function (config) {
//     if (config.data && !(config.data instanceof FormData) && typeof config.data !== 'string') {
//         config.headers['Authorization'] = `Bearer ${sessionStorage.getItem('token')}`;
//         config.data = qs.stringify(config.data);
//     }
//     return config;
// }, function (error) {
//     return Promise.reject(error);
// });

// // Add a response interceptor
// http.interceptors.response.use(function (response) {
//     return response.data;
// }, function (error) {
//     return Promise.reject(error);
// });

// export default http;
